package com.tuzhi.filestore.scheduled;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.tuzhi.base.web.vo.AjaxResultVO;
import com.tuzhi.filestore.config.FileStoreConfig;
import com.tuzhi.filestore.http.BillApiClient;
import com.tuzhi.filestore.websocket.server.WebSocketHelp;
import com.tuzhi.filestore.websocket.vo.OrderStatusMsgVo;
import com.tuzhi.vo.bill.DocAutoDto;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Slf4j
@Component
public class DocAutoCheckScheduled {
    @Resource
    BillApiClient billApiClient;
    @Autowired
    FileStoreConfig fileStoreConfig;

//    @Scheduled(cron = "0 */2 * * * ?")
    public void task() {
        log.info("=========定时任务推送记录到自动路线客户端开始 组织={}========", fileStoreConfig.getOrg());
        String todayStr = DateUtil.today();
        Date startDate = DateUtil.parse(todayStr, "yyyy-MM-dd");
        Date endDate = DateUtil.parse(String.format("%s 23:59:59", todayStr), "yyyy-MM-dd HH:mm:ss");
        Date now = new Date();
        int count4Success = 0;
        int count4Fail = 0;
        AjaxResultVO<List<DocAutoDto>> result = billApiClient.docAutoCheckList(fileStoreConfig.getOrg(), "unentered", startDate, endDate);
        if (result.isSuccess()) {
            List<DocAutoDto> list = result.getData();
            if (CollectionUtils.isNotEmpty(list)) {
                for (DocAutoDto docAutoDto : list) {
                    if (DateUtil.between(docAutoDto.getCreateTime(), now, DateUnit.MINUTE) >= 5) {
                        OrderStatusMsgVo orderStatusMsgVo = new OrderStatusMsgVo();
                        orderStatusMsgVo.setStatus("sysAutoCheck");
                        orderStatusMsgVo.setModelId(docAutoDto.getModelId());
                        orderStatusMsgVo.setSource("app001");
                        try {
                            Map<String, Integer> resultMap = WebSocketHelp.sendOrderStatus(orderStatusMsgVo);
                            if (MapUtils.getInteger(resultMap, "AutoCheckClient", 0) <= 0) {
                                count4Fail++;
                            } else {
                                count4Success++;
                            }
                        } catch (Exception e) {
                            log.error("定时任务推送记录到自动路线客户端", e);
                        }
                    }
                }
            }
        }
        if (count4Fail > 0) {
            String msg = String.format("组织=%s，自动路线客户端推送成功：%s,失败：%s", fileStoreConfig.getOrg(), count4Success, count4Fail);
//            log.info(msg);
            billApiClient.sendWarmMail(0l, msg, msg);
        }
        log.info("=========定时任务推送记录到自动路线客户端结束，成功推送{}，失败{}========", count4Success, count4Fail);
    }

}
